#include <DHT.h>

/****************************************/
/*   Hydrophonics by Neomar Giacomini   */
/*   Saint Joseph, Mi, USA  2014        */
/****************************************/
/****************************************/
/*01/03/15*/
/* # Project members authenticate over HTTPS to allow committing changes.*/
/*svn checkout https://hydroponics.googlecode.com/svn/trunk/ hydroponics --username vinicius.passini@gmail.com*/
/*When prompted, enter your generated googlecode.com password.*/
/*Use this command to anonymously check out the latest project source code:*/
/*# Non-members may check out a read-only working copy anonymously over HTTP.*/
/*svn checkout http://hydroponics.googlecode.com/svn/trunk/ hydroponics-read-only*/

// Defining libraries
#include <DmxMaster.h>
#include <TimedAction.h>
#include <FiniteStateMachine.h>

TimedAction timedAction = TimedAction(1,milisec);
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11 
DHT dht(DHTPIN, DHTTYPE);

/****************************************/
/*       STATE MACHINE STATES           */
/****************************************/
State AirPumpOn = State(AirOn); 
State AirPumpOff = State(AirOff);
State WaterPumpOn = State(WaterOn);
State WaterPumpOff = State(WaterOff);

FSM stateMachineAir = FSM(AirPumpOff);
FSM stateMachineWater = FSM(WaterPumpOff);

/****************************************/
/*                                      */
/*  Roda a cada 1ms para gerar máquina  */
/*                                      */
/****************************************/

int IContaMilisec=0; 
int IContaDuzentosCinquentaMilisec=0; 
int IContaQuinhentosMilisec=0; 
int IContaSegundos=0;
int IConta10Seg=0;

/****************************************/
/*                                      */
/*  	Relative Humidity 	        */
/*                                      */
/****************************************/
float h;
/****************************************/
/****************************************/
/*  The setup function runs once when   */
/*  you press reset or power the board  */
/****************************************/

void setup() 
{
  pinMode(13, OUTPUT);    // IO13 (PC7) as an output.
  pinMode(6, OUTPUT);     // D6 (PD7) as an output.
  pinMode(7,OUTPUT);
  DmxMaster.maxChannel(10);    /*set the max channel, in this way that the module doesn't have to cycle through all the possible channels every time*/
  Serial.begin(9600);
  dht.begin();
}

/****************************************/
/*  Definindo algumas variáveis         */
/****************************************/

int ON = 1;
int OFF = 0;

/****************************************/
/*  The loop function runs over and     */
/*  over again forever                  */
/****************************************/

void loop() 
{
  timedAction.check();   // Update timer for the scheduler
}



void milisec()
{
  IContaMilisec++;
  if(IContaMilisec >= 250)
    {
      IContaDuzentosCinquentaMilisec++;
      DuzentosCinquenta();
      IContaMilisec = 0;
    }
  if(IContaDuzentosCinquentaMilisec >= 2)
    {
      IContaQuinhentosMilisec++;
      Quinhentos();
      IContaDuzentosCinquentaMilisec = 0;
    }
  if(IContaQuinhentosMilisec >= 2)
    {
      IContaSegundos++;
      Segundo();
      IContaQuinhentosMilisec = 0;
    }  
  if(IContaSegundos >= 10)
    {
      DezSegundos();
      IContaSegundos = 0;
    }
  if(IConta10Seg >= 6)
    {
      Minuto();
      IConta10Seg = 0;
    }

// Código que deva rodar a cada 1ms deve ficar aqui abaixo.
//
//
//

}

/****************************************/
/*  Roda a cada 250ms, loop tarefas     */
/****************************************/

void DuzentosCinquenta()
{

}

/****************************************/
/*  Roda a cada 500ms, loop tarefas     */
/****************************************/

void Quinhentos()
{
 
}

/****************************************/
/*  Roda a cada 1s                      */
/****************************************/

void Segundo()
{
  h = dht.readHumidity();
  Serial.print(h);
  Serial.print(" %\t\r\n");
}

/****************************************/
/*  Roda a cada 10s                      */
/****************************************/

void DezSegundos()
{
  DmxMaster.write(5,255);
  //digitalWrite(7,!iEstadoLEDy);
  Serial.print("10 SECONDS\t\r\n");
  h = dht.readHumidity();
  if(h<44)
  {
    stateMachineAir.transitionTo(AirPumpOn);
    stateMachineWater.transitionTo(WaterPumpOn);
    Serial.print("HIGH HUMIDITY\t\r\n");    
  }
  else
  {
    stateMachineAir.transitionTo(AirPumpOff);
    stateMachineWater.transitionTo(WaterPumpOff);
    Serial.print("LOW HUMIDITY\t\r\n");
  }
  stateMachineAir.update();
  stateMachineWater.update();
  //  DmxMaster.write(5,182);
}
void Minuto()
{
  //DmxMaster.write(4,!iEstadoRele);
  //iEstadoRele = !iEstadoRele;
  
  //if(iEstadoRele == 0) { DmxMaster.write(8,0); }
  //if(iEstadoRele == 1) { DmxMaster.write(8,255); }  
}
void AirOn()
{
  digitalWrite(13,ON);
  Serial.print("AIR ON \t\r\n");
}
void AirOff()
{
  digitalWrite(13,OFF);
  Serial.print("AIR OFF \t\r\n");
}
void WaterOn()
{
  digitalWrite(7,ON);
  Serial.print("WATER ON \t\r\n");
}
void WaterOff()
{
  digitalWrite(7,OFF);
  Serial.print("WATER OFF \t\r\n");
}

//*********************************************************************
//*********************************************************************
//*******                                                    **********
//*******                     PARKING LOT                    **********
//*******                                                    **********
//*********************************************************************
//*********************************************************************


  
/*  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  DmxMaster.write(1, HIGH);
  delay(500);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  DmxMaster.write(1, LOW);
  delay(500);              // wait for a second
  */
